import { login } from './api/index';
import config from './config';

App({
  getToken() {
    return new Promise((resolve, reject) => {
      const token = wx.getStorageSync(config.TOKEN_KEY);
      if (token) {
        resolve(token);
      } else {
        wx.login({
          success: res => {
            login({
              data: {
                code: res.code
              },
              success: res => {
                const { token } = res.data.data;
                wx.setStorageSync(config.TOKEN_KEY, token);
                resolve(token);
              },
              fail: res => {
                reject(res);
              }
            });
          }
        });
      }
    });
  }
});
